<script setup>
import {ref} from "vue";
import request from '../../utils/request';
import { ElMessage, ElMessageBox } from 'element-plus'
// 定义响应式数据
const Account = ref({
  username: '',
  password: '',
  newPassword: '',
  confirmPassword: ''
})
// 验证确认密码
const validatePassword = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'));
  } else if (value !== Account.value.newPassword) {
    callback(new Error('两次输入密码不一致!'));
  } else {
    callback();
  }
};
// 表单验证规则
const rules = {
  password: [{ required: true, message: '请输入原始密码', trigger: 'blur' }],
  newPassword: [{ required: true, message: '请输入新密码', trigger: 'blur' }],
  confirmPassword: [ { validator: validatePassword, required: true, trigger: 'blur' },],
};
// 定义响应式数据
const user = ref(JSON.parse(localStorage.getItem('happyuser') || '{}'));

const update = () => {
  Account.value.username =  user.value.username
  request({
    url:  '/updatePassword' ,
    method: 'put' , // 根据id是否存在来判断是put还是post
    data: Account.value
   }).then((res) => {
    //console.log(1111)
    if(res.data.code == 200){
      ElMessage({
      message: '更改成功',
      type: 'success',
     })
    //  localStorage.setItem('happyuser',JSON.stringify(user.value))
     // 刷新页面
    //  window.location.reload();
    }else{
      //console.log(11112)
      ElMessage.error('更改失败')
    } 
   })
}
</script>

<template>
    <div class="main">
      <el-card style="width: 50%" class="elcard">
        <el-form  :model="Account" :rules="rules" label-width="100px" style="padding-right: 50px">
          <el-form-item label="原始密码" prop="password">
            <el-input show-password v-model="Account.password" placeholder="原始密码"></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="newPassword">
            <el-input show-password v-model="Account.newPassword" placeholder="新密码"></el-input>
          </el-form-item>
          <el-form-item label="确认新密码" prop="confirmPassword">
            <el-input show-password v-model="Account.confirmPassword" placeholder="确认密码"></el-input>
          </el-form-item>
          <div style="text-align: center; margin-bottom: 20px">
            <el-button type="primary" @click="update">确认修改</el-button>
          </div>
        </el-form>
      </el-card>
    </div>
  </template>
  
  <style scoped>
.main{
  width: 100%;
  height: 100%;
  margin: 0px auto;
  padding-top: 100px;
  background-image: url(../assets/pbeijing.png);
   background-size: cover;

}
.elcard{
  width: 55%;
  /* height: ; */
  margin: 0px auto;
  border-radius: 20px;
  /* background-color: rgb(246, 246, 246); */
  background-color: transparent;
}
  </style>